﻿using System.Runtime.Serialization;

namespace Tp.Dto.Allgemein
{
	  public class AddressDto : InPcBase, IAbstractDto
	  {

			#region Members

			private bool isDirty = false;
			private bool canUndo = false;

			private AddressTypeDto addressType;
			private AddressTypeDto addressTypeOld;
			private string plz;
			private string plzOld;
			private string ort;
			private string ortOld;
			private string strasse;
			private string strasseOld;
            private string land;
            private string landOld;
			private string hausnummer;
			private string hausnummerOld;
			private string vermerk;
			private string vermerkOld;
			private int zone;
			private int zoneOld;
			private decimal entfernung;
			private decimal entfernungOld;

			#endregion

			#region Properties

			[DataMember]
			public int AddressDtoId { get; set; }

			[DataMember]
			public AddressTypeDto AddressType {
				  get { return this.addressType; }
				  set
				  {
						if (this.addressType == value)
						{
							  return;
						}

						if (this.addressType != value)
						{
							  this.IsDirty = true;
						}
						this.addressTypeOld = this.addressType;
						this.addressType = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => AddressType);
				  }
			}

			[DataMember]
			public string Plz {
				  get { return this.plz; }
				  set
				  {
						if (this.plz == value)
						{
							  return;
						}

						if (this.plz != value)
						{
							  this.IsDirty = true;
						}
						this.plzOld = this.plz;
						this.plz = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Plz);
				  }
			}

			[DataMember]
			public string Ort {
				  get { return this.ort; }
				  set
				  {
						if (this.ort == value)
						{
							  return;
						}

						if (this.ort != value)
						{
							  this.IsDirty = true;
						}
						this.ortOld = this.ort;
						this.ort = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Ort);
				  }
			}

			[DataMember]
			public string Strasse {
				  get { return this.strasse; }
				  set
				  {
						if (this.strasse == value)
						{
							  return;
						}

						if (this.strasse != value)
						{
							  this.IsDirty = true;
						}
						this.strasseOld = this.strasse;
						this.strasse = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Strasse);
				  }
			}

            [DataMember]
            public string Land {
                get { return this.land; }
                set {
                    if (this.land == value) {
                        return;
                    }

                    if (this.land != value) {
                        this.IsDirty = true;
                    }
                    this.landOld = this.land;
                    this.land = value;
                    this.CanUndo = true;
                    RaisePropertyChanged(() => Land);
                }
            }

			[DataMember]
			public string Hausnummer {
				  get { return this.hausnummer; }
				  set
				  {
						if (this.hausnummer == value)
						{
							  return;
						}

						if (this.hausnummer != value)
						{
							  this.IsDirty = true;
						}
						this.hausnummerOld = this.hausnummer;
						this.hausnummer = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Hausnummer);
				  }
			}

			[DataMember]
			public string Vermerk { get; set; }

			[DataMember]
			public int Zone { get; set; }

			[DataMember]
			public decimal Entfernung { get; set; }

			public bool HasValue
			{
				  get { return Ort != null; }
			}

			public bool IsDirty
			{
				  get { return this.isDirty; }
				  set
				  {
						this.isDirty = value;
						RaisePropertyChanged(() => IsDirty);
				  }
			}

			public bool CanUndo
			{
				  get { return this.canUndo; }
				  set
				  {
						this.canUndo = value;
						RaisePropertyChanged(() => CanUndo);
				  }
			}

			#endregion

			#region C-Tor

			public AddressDto(
				  int addressId, 
				  AddressTypeDto addressType,
				  string plz,
				  string ort,
				  string strasse,
				  string hausnummer,
				  string vermerk,
				  int zone,
				  decimal entfernung,
                  string land
                )
			{
				  this.AddressDtoId = addressId;
				  this.addressType = addressType;
				  this.plz = plz;
				  this.ort = ort;
				  this.strasse = strasse;
				  this.vermerk = vermerk;
				  this.hausnummer = hausnummer;
				  this.zone = zone;
				  this.entfernung = entfernung;
                  this.land = land;
			}

			#endregion

			#region Methods

			public void UndoChanges()
			{
				  this.AddressType = (this.addressTypeOld == null ? this.AddressType : this.addressTypeOld);
				  this.Plz = (this.plzOld == null ? this.Plz : this.plzOld);
				  this.Ort = (this.ortOld == null ? this.Ort : this.ortOld);
				  this.Strasse = (this.strasseOld == null ? this.Strasse : this.strasseOld);
				  this.Vermerk = (this.vermerkOld == null ? this.Vermerk : this.vermerkOld);
				  this.Hausnummer = (this.hausnummerOld == null ? this.Hausnummer : this.hausnummerOld);
				  this.Zone = (this.zoneOld == null ? this.Zone : this.zoneOld);
				  this.Entfernung = (this.entfernungOld == null ? this.Entfernung : this.entfernungOld);
				  this.CanUndo = false;
			}

			#endregion

			#region Overrides

			public override string ToString()
			{
				  return Plz + " - " + Ort + ", " + Strasse;
			}

			#endregion

	  }
}
